package com.sz.jinzheng.service.impl;

import com.sz.jinzheng.mapper.ManagerMapper;
import com.sz.jinzheng.mapper.StaffExminationMapper;
import com.sz.jinzheng.model.BorrowerInfo;
import com.sz.jinzheng.model.StaffExmination;
import com.sz.jinzheng.model.Task;
import com.sz.jinzheng.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author qin
 * @version 1.0
 * @description: TODO
 * @date 2021/7/21 19:56
 */
@Service
public class ManagerServiceImpl implements ManagerService {
    @Autowired
    private ManagerMapper managerMapper;
    @Autowired
    private StaffExminationMapper staffExminationMapper;

    //通过任务id查询审查专员审查记录
    @Override
    public List<StaffExmination> getReviewByTid(Task task) {
        return managerMapper.getReviewByTid(task);
    }

    /**
     * @author qin
     * @date
     * @version 1.0
     * @description:
     */
    @Transactional
    @Override
    public int insertReviewRecord(StaffExmination staffExmination) {
        //更新审批表
        int i1 = staffExminationMapper.updateStaffExmination(staffExmination);
        //查询出审批表中的信息
        StaffExmination exminationById = staffExminationMapper.getStaffExminationById(staffExmination.getId());
        //根据taskId更改任务表t_task的status状态为8
        Task task=new Task(exminationById.getTaskId(),8);
        int i2 = managerMapper.updateTaskStatusById(task);

        int i3=0;
        if (staffExmination.getStatus()==1){//审核通过
            BorrowerInfo borrowerInfo=new BorrowerInfo(staffExmination.getBorrowerInfoId(),2);
            //根据id修改借款人信息表t_borrower_info中的status为2，表示审批通过
            i3= managerMapper.updateBorrowerInfoStatusById(borrowerInfo);

        }else {//审核不通过
            BorrowerInfo borrowerInfo=new BorrowerInfo(staffExmination.getBorrowerInfoId(),-1);
            //根据id修改借款人信息表t_borrower_info中的status为-1，表示不通过
            i3 = managerMapper.updateBorrowerInfoStatusById(borrowerInfo);
        }
        if (i1>0&&i2>0&&i3>0){//三个表的记录修改成功
            return 1;
        }
        return 0;
    }
}
